<?php

/**
 * 模板标签文件
 */
class TagLibTpl extends TagLib {

    protected $tags = array(
        'useTPL' => array('attr'    => 'id,formtype,field,value,name', 'level'   => 3),
        'setType' => array('attr'   => 'name', 'level'  => 3),
        'setVal' => array('attr'    => 'id,name,func', 'level'   => 3),
        'showTPL' => array('attr'     => 'name', 'level'    => 3),
        'show_ext' => array('attr'  => 'type', 'close' => 0),
    );

    public function _useTPL($attr, $content) {
        $parseStr = '<?php ';
        if (!class_exists('content_form')) {
            require_cache(LIB_PATH . 'ORG/Fields/content_form_cache.class.php');
            $parseStr .= '$_tpl_content_form = new content_form();$_tpl_form_data=array();';
        }
        $tag = $this->parseXmlAttr($attr);
        $parseStr .= '$_tpl_form_setting = array();';
        $parseStr .= '$_tpl_form_setting["id"] = "' . $tag['id'] . '";';
        $parseStr .= '$_tpl_form_setting["formtype"] = "' . $tag['formtype'] . '";';
        $parseStr .= '$_tpl_form_setting["field"] = "' . $tag['field'] . '";';
        $parseStr .= '$_tpl_form_setting["value"] = ' . $tag['value'] . ';';
        $parseStr .= '$_tpl_form_setting["name"] = "' . $tag['name'] . '";?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .= '<?php $_tpl_form_setting["setting"] = serialize($_tpl_form_setting["setting"]);';
        $parseStr .= '$tmp = $_tpl_content_form->$_tpl_form_setting[\'formtype\']($_tpl_form_setting[\'field\'], $_tpl_form_setting["value"], $_tpl_form_setting);';
        $parseStr .='if ($tmp !== false) {';
        $parseStr .='    $_tpl_star = $_tpl_form_setting[\'minlength\'] || $_tpl_form_setting[\'pattern\'] ? 1 : 0;';
        $parseStr .='$_tpl_form_data[$_tpl_form_setting[\'field\']] = array(';
        $parseStr .='       \'name\'         => $_tpl_form_setting[\'name\'],
                            \'tips\'         => $_tpl_form_setting[\'tips\'],
                            \'form\'         => $tmp,
                            \'star\'         => $_tpl_star,
                            \'isomnipotent\' => $_tpl_form_setting[\'isomnipotent\'],
                            \'formtype\'     => $_tpl_form_setting[\'formtype\']
                            );}?>';
        return $parseStr;
    }

    public function _setType($attr, $content) {
        $tag      = $this->parseXmlAttr($attr);
        $parseStr = $this->tpl->parse($content);
        return $parseStr;
    }

    public function _setVal($attr, $content) {
        $tag      = $this->parseXmlAttr($attr);
        $parseStr = '<?php $_tpl_form_setting' . $tag['id'] . ' = "' . $content . '";?>';
        return $parseStr;
    }

    public function _showTPL($attr, $content) {
        $tag      = $this->parseXmlAttr($attr);
        $parseStr = '<?php $form = $_tpl_form_data["' . $tag['name'] . '"];?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .='<?php unset($form);?>';
        return $parseStr;
    }

    public function _show_ext($attr) {
        $tag = $this->parseXmlAttr($attr);
        switch ($tag['type']) {
            case "formValidateRules":
                //生成对应的JS验证规则
                $parseStr = '<?php echo ($_tpl_content_form->formValidator ? $_tpl_content_form->formValidator : "{}");?>' . "\r\n";
                break;
            case "formValidateMessages":
                //js验证不通过提示语
                $parseStr = '<?php echo ($_tpl_content_form->formValidateMessages ? $_tpl_content_form->formValidateMessages : "{}");?>' . "\r\n";
                break;
            case "formJavascript":
                //js
                $parseStr = '<?php echo $_tpl_content_form->formJavascript;?>' . "\r\n";
                break;
        }
        return $parseStr;
    }

}
